package com.xymtop.api.controller;

import com.xymtop.api.config.ServerStatus;
import com.xymtop.api.entity.PStudent;
import com.xymtop.api.service.PStudentService;
import com.xymtop.api.utils.JsonResoult;
import com.xymtop.api.utils.Timer;
import com.xymtop.api.vo.StuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName : PStudentController
 * @Description : 学生信息控制类
 * @Author : 肖叶茂
 * @Date: 2022/12/1  16:42
 */

@RestController
@RequestMapping("pstudent")
public class PStudentController {

    @Autowired
    private PStudentService pStudentService;

    @RequestMapping("add")
    public JsonResoult<String> add(@RequestParam("projectid") String projectid, @RequestParam("userid") String userid, @RequestParam("head") String head, @RequestParam("rank") String rank) {

        PStudent pStudent = new PStudent();
        pStudent.setUserid(userid);
        pStudent.setProjectid(projectid);
        pStudent.setHead(head);
        pStudent.setRank(rank);
        pStudent.setTime(Timer.getSmapleTime(Timer.getTimeStamp()));

        if(pStudentService.add(pStudent)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }


    }



    @RequestMapping("delete")
    public JsonResoult<String> delete(@RequestParam("projectid") String projectid, @RequestParam("userid") String userid) {
        if(pStudentService.delete(projectid, userid)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }


    @RequestMapping("changeRank")
    public JsonResoult<String> changeRank(@RequestParam("projectid") String id,@RequestParam("userid") String userid,
                                          @RequestParam("rank") String rank) {
        if(pStudentService.changeRank(id,userid, rank)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }

    @RequestMapping("changeHead")
    public JsonResoult<String> changeHead(@RequestParam("projectid") String id,@RequestParam("userid") String userid,
                                          @RequestParam("head") String head) {
        if(pStudentService.changeHead(id, userid, head)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }


    @RequestMapping("get")
    public JsonResoult<PStudent> get(@RequestParam("projectid") String projectid, @RequestParam("userid") String userid) {
       return new JsonResoult<>(200,"",pStudentService.get(projectid, userid));
    }

    @RequestMapping("getStuList")
    public JsonResoult<List<StuVO>> getStuList(@RequestParam("projectid") String projectid) {
        return new JsonResoult<>(200,"",pStudentService.getStuList(projectid));
    }
}
